package com.Utility;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences.Editor;
import android.os.Build;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;

public class Utility {
	public static String NETWORK_ISSUE = "Please check your network setting.";
	public static String INVALID_USER = "Invalid username or password.";
	public static String KEY_LOGIN_TOKEN = "LoginToken";
	public static String KEY_PASSWORD = "password";

	// public static
	public static String getDeviceId(Context ctx) {

		TelephonyManager telephonyManager = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		String mDeviceID = telephonyManager.getDeviceId();

		if (mDeviceID == null || mDeviceID.equals("000000000000000")) {
			mDeviceID = "101";
			return mDeviceID;
		} else
			return mDeviceID;

		// String mDeviceID =
		// Secure.getString(ctx.getContentResolver(),Secure.ANDROID_ID);
		// if(mDeviceID==null)
		// {
		// mDeviceID="01235";
		// }
		// String mDeviceID = android.telephony.TelephonyManager.getDeviceId();
		// return "01235";

	}

	public static String getAndroidVersion(Context ctx) {
		String version = Build.VERSION.RELEASE;
		if (version == null || version.equals(""))
			return "Not Available";
		else
			return version;
	}

	public static String getAndroidModel(Context ctx) {
		String model = Build.MODEL;
		if (model == null || model.equals(""))
			return "Not Available";
		else
			return model;

	}

	public static String getMobileNumber(Context ctx) {
		TelephonyManager telephonyManager = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		String phone = telephonyManager.getLine1Number();
		if (phone != null && phone.length() > 0) {
			Utility.setStringPreference(ctx, "Phone_Number", phone);
		} else {
			Utility.setStringPreference(ctx, "Phone_Number", "NA");
		}

		if (phone == null || phone.equals("")) {
			return "NA";
		} else {
			return phone;
		}

	}

	public static void printLog(String tag, String msg) {
		System.out.println("tag:: " + msg);
	}

	public static String getStringPreference(Context context, String pref,
			String def) {

		return PreferenceManager.getDefaultSharedPreferences(context)
				.getString(pref, def);

	}

	public static int getIntPreference(Context context, String pref, int def) {
		return PreferenceManager.getDefaultSharedPreferences(context).getInt(
				pref, def);
	}

	public static long getLongPreference(Context context, String pref, long def) {
		return PreferenceManager.getDefaultSharedPreferences(context).getLong(
				pref, def);
	}

	public static void setIntPreference(Context c, String pref, int val) {
		Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
		e.putInt(pref, val);
		e.commit();
	}

	public static void setStringPreference(Context c, String pref, String val) {
		Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
		e.putString(pref, val);
		e.commit();
	}

	public static void setLongPreference(Context c, String pref, Long val) {
		Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
		e.putLong(pref, val);
		e.commit();
	}

	public static boolean getBooleanPreference(Context context, String pref,
			boolean def) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getBoolean(pref, def);
	}

	public static void setBooleanPreference(Context c, String pref, boolean val) {
		Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
		e.putBoolean(pref, val);
		e.commit();
	}

	public static boolean isEmailValid(String email) {
		boolean isValid = false;

		String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
		CharSequence inputStr = email;

		Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(inputStr);
		if (matcher.matches()) {
			isValid = true;
		}
		return isValid;
	}

	public static void showAlert(Context context, String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(msg).setTitle("Alert").setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alertDialog = builder.create();
		alertDialog.show();
	}

	public static boolean isExternalStorageAvailable() {
		boolean state = false;
		String extStorageState = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
			state = true;
		}
		return state;
	}
}
